﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml;
using System.Xml.Linq;
using Sever.Models;
using System.Web;
using System.Threading.Tasks;
using System.Diagnostics;
using HtmlAgilityPack;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver.GridFS;
using MongoDB.Driver.Linq;
using System.Text;
using System.IO;
using System.Data;



namespace Sever.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
 
    public class ValuesController : ApiController
    {
        string rootdata = HttpContext.Current.Server.MapPath("~/App_Data");
        string rootimage = HttpContext.Current.Server.MapPath("~/Images");
        [HttpGet]
        public bool DelPost(string DelPost, string id)
        {
            bool a = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/NoiDungComment.XML");
            try
            {
                string query = "Delete from  BaiDang " +
                    "where BaiDang.ID=" +id;
                DataProvider.ExecuteQuery(query);
                query = "Delete from  Comment1 " +
                    "where Comment1.ID=" +id;
                DataProvider.ExecuteQuery(query);

                    XmlNodeList node = Doc.SelectNodes("//Comment[Code='" + id + "']");
                    if(node.Count!=0)
                    {
                        node[0].ParentNode.RemoveChild(node[0]);
                    }
                    a=true;
                
            }
            catch (Exception ex)
            {
                return false;
            }
            return a;
        }
        [HttpGet]
        public bool DelComment(string DelComment,string stt)
        {
            bool A = false;
            List<ReturnComment> list = new List<ReturnComment>();
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/NoiDungComment.XML");
            try
            {
                string query = "delete from  Comment1 " +
                    "where ID="+stt;
                DataProvider.ExecuteQuery(query);

                    XmlNodeList node = Doc.SelectNodes("//Comment[Code='" + stt + "']");
                   if(node.Count!=0)
                   {
                       node[0].ParentNode.RemoveChild(node[0]);
                   }
                   Doc.Save(rootdata + "/NoiDungComment.XML");
                   A = true;
            }
            catch (Exception ex)
            {
                return false;
            }
            return A;
        }
        [HttpGet]
        public List<ReturnComment> AllComment(string AllComment)
        {
            List<ReturnComment> list = new List<ReturnComment>();
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/NoiDungComment.XML");
            try
            {
                string query = "select  BaiDang.TuaDe,Comment1.Code,Comment1.NgayDang "+
                    "from BaiDang,Comment1 "+
                    "where BaiDang.ID=Comment1.ID";
                DataTable TB = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in TB.Rows)
                {
                    ReturnComment dto = new ReturnComment();
                    dto.TuaDe = dr["TuaDe"].ToString();
                    dto.Code = dr["Code"].ToString();
                    dto.NgayDang = dr["NgayDang"].ToString();

                    XmlNodeList node = Doc.SelectNodes("//Comment[Code='" + dto.Code + "']");
                    for (int i = 0; i < node.Count; i++)
                    {
                        for (int j = 0; j < node[i].ChildNodes.Count; j++)
                        {
                            if (j == 1)
                                dto.NoiDung = node[i].ChildNodes[j].InnerText;
                            if (j == 2)
                                dto.NguoiDang = node[i].ChildNodes[j].InnerText;
                        }
                    }
                    list.Add(dto);
                }
            }
            catch (Exception ex)
            {
                return list;
            }
            return list;
        }
       [HttpGet] 
        public bool EditUser(string name,string newpass, string url)
       {
           bool A = false;
           try
           {
               UpdateAvatar(name,url);
               XmlDocument Doc = new XmlDocument();
               Doc.Load(rootdata + "/Database.XML");
               XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name + "']");
               for (int i = 0; i < node.Count; i++)
               {
                   XmlNode nodeLH = Doc.DocumentElement;
                   XmlNode nodeSV = Doc.CreateElement("User");

                   XmlAttribute HT = Doc.CreateAttribute("UserName");
                   HT.Value = node[0].Attributes["UserName"].Value;
                   XmlAttribute Type = Doc.CreateAttribute("Type");
                   Type.Value = node[0].Attributes["Type"].Value;
                   XmlAttribute Pa = Doc.CreateAttribute("Pass");
                   Pa.Value = newpass;
                   XmlAttribute Av = Doc.CreateAttribute("Avatar");
                   Av.Value = node[0].Attributes["Avatar"].Value;
                   XmlAttribute stt = Doc.CreateAttribute("status");
                   stt.Value = node[i].Attributes["status"].Value;
                   XmlAttribute yh = Doc.CreateAttribute("yh");
                   yh.Value = node[i].Attributes["yh"].Value;

                   nodeSV.Attributes.Append(yh);
                   nodeSV.Attributes.Append(Type);
                   nodeSV.Attributes.Append(HT);
                   nodeSV.Attributes.Append(Pa);
                   nodeSV.Attributes.Append(Av);
                   nodeSV.Attributes.Append(stt);

                   for (int j = 0; j < node[0].ChildNodes.Count; j++)
                   {
                       XmlNode temp = Doc.CreateElement("Friend");
                       temp.InnerText = node[0].ChildNodes[j].InnerText;
                       nodeSV.AppendChild(temp);
                   }

                   node[i].ParentNode.ReplaceChild(nodeSV, node[i]);

               }
               A = true;
               Doc.Save(rootdata + "/Database.XML");
           }
           catch (Exception ex)
           {
               return false;
           }
           return A;
       }
        [HttpGet]
        public bool DelUser(string DelUser)
        {
            bool a = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + DelUser + "']");
                if (node.Count <= 0)
                {
                    return false;
                }
                else
                {
                    node[0].ParentNode.RemoveChild(node[0]);
                    Doc.Save(rootdata + "/Database.XML");
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return a;
        }
        [HttpGet]
        public List<Users> AllUser(string AllUser)
        {
            List<Users> list = new List<Users>();
            XmlDocument Doc = new XmlDocument();
            string Name = "";
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User");
                if (node.Count <= 0)
                {

                }
                else
                {
                    for (int i = 0; i < node.Count; i++)
                    {
                        Users user = new Users();
                        user.ID = i + 1;
                        user.Name = node[i].Attributes["UserName"].Value;
                        user.avatar = node[i].Attributes["Avatar"].Value;
                        user.stt = node[i].Attributes["status"].Value;
                        user.Type = node[i].Attributes["Type"].Value;
                        list.Add(user);
                    }
                        
                }
            }
            catch (Exception ex)
            {
                return list;
            }
            return list;
        }
        [HttpGet]
        public bool ReturnAdmin(string username,string pass,string admin)
        {
            bool A = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + username + "' and @Pass='" + pass + "' and @Type='1']");
                if (node.Count <= 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return A;
        }
        [HttpGet]
        public List<Comment> ReturnComment(string ID,string cmnt)
        {
            List<Comment> list = new List<Comment>();
            try
            {
                XmlDocument Doc = new XmlDocument();
                Doc.Load(rootdata + "/NoiDungComment.XML");

                string query = "select * from Comment1  where ID=" + ID.ToString() +" order by  Code asc";
                DataTable TB = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in TB.Rows)
                {
                    Comment dto = new Comment();
                    dto.ID = dr["ID"].ToString();
                    dto.Code = dr["Code"].ToString();
                    dto.NgayDang = dr["NgayDang"].ToString();

                    XmlNodeList node = Doc.SelectNodes("//Comment[Code='" + dto.Code + "']");
                    for (int i = 0; i < node.Count; i++)
                    {
                        for (int j = 0; j < node[i].ChildNodes.Count; j++)
                        {
                            if (j == 1)
                                dto.NoiDung = node[i].ChildNodes[j].InnerText;
                            if (j == 2)
                                dto.NguoiDang = node[i].ChildNodes[j].InnerText;
                            if (j == 3)
                                dto.Avatar = node[i].ChildNodes[j].InnerText;
                        }
                    }
                    list.Add(dto);
                }
            }
            catch (Exception ex)
            {
                return list; 
            }
            return list; 
        }
        [HttpGet]
        public bool DangComment(string ID,string NguoiDang,string NoiDung,string Avatar)
        {
            bool a=false;
            int code = -1;
            string quer= "SELECT MAX(Code) from Comment1";
            DataTable TB = DataProvider.ExecuteQuery(quer);
            foreach (DataRow dr in TB.Rows)
            {
               code = Convert.ToInt16(dr[0]) + 1;
            }
            string NgayDang =DateTime.Now.Day + "/" + DateTime.Now.Month + "/" + DateTime.Now.Year;
            try
            {
                string query = " insert into Comment1 Values('" + code.ToString() + "','" + ID.ToString() + "','" + NgayDang +  "')";
                DataProvider.ExecuteQuery(query);
                XmlDocument Doc = new XmlDocument();
                Doc.Load(rootdata + "/NoiDungComment.XML");

                XmlNode nodeLH = Doc.DocumentElement;
                XmlNode nodeSV = Doc.CreateElement("Comment");

                XmlNode Code = Doc.CreateElement("Code");
                Code.InnerText = code.ToString();
                XmlNode noiDung = Doc.CreateElement("NoiDung");
                noiDung.InnerText = NoiDung;
                XmlNode nguoidang = Doc.CreateElement("NguoiDang");
                nguoidang.InnerText = NguoiDang;
                XmlNode avatar = Doc.CreateElement("Avatar");
                avatar.InnerText = Avatar;

                nodeSV.AppendChild(Code);
                nodeSV.AppendChild(noiDung);
                nodeSV.AppendChild(nguoidang);
                nodeSV.AppendChild(avatar);
                nodeLH.AppendChild(nodeSV);

                Doc.Save(rootdata + "/NoiDungComment.XML");
                a = true;

            }
            catch (Exception ex) 
            { return false; };
            return a;
        }
        [HttpGet]
        public BaiDang ReturnPostByID(string ID)
        {
             BaiDang dto = new BaiDang();
            try 
            { 
                XmlDocument Doc = new XmlDocument();
                Doc.Load(rootdata + "/NoiDungBaiDang.XML");

                string query = "select * from BaiDang where ID="+ID;
                DataTable TB = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in TB.Rows)
                {                 
                    dto.ID = dr["ID"].ToString();
                    dto.TuaDe= dr["TuaDe"].ToString();
                    dto.NgayDang = dr["NgayDang"].ToString();
                    dto.NguoiDang = dr["NguoiDang"].ToString();

                   XmlNodeList node = Doc.SelectNodes("//BaiDang[ID='" + dto.ID + "']");
                   for (int i = 0;i<node.Count;i++ )
                    {
                        for (int j = 0; j < node[i].ChildNodes.Count; j++)
                        {
                            if (j == 1)
                                dto.NoiDung = node[i].ChildNodes[j].InnerText;
                        }
                    }
                }
            }
            catch (Exception ex) { return null; };
            return dto;
        }
        [HttpGet]
        public List<BaiDang> ReturnPost(string post)
        {
            List<BaiDang> list = new List<BaiDang>();
            try
            {
                XmlDocument Doc = new XmlDocument();
                Doc.Load(rootdata + "/NoiDungBaiDang.XML");

                string query = "select * from BaiDang order by ID desc";
                DataTable TB = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in TB.Rows)
                {
                    BaiDang dto = new BaiDang();
                    dto.ID = dr["ID"].ToString();
                    dto.TuaDe= dr["TuaDe"].ToString();
                    dto.NgayDang = dr["NgayDang"].ToString();
                    dto.NguoiDang = dr["NguoiDang"].ToString();

                   XmlNodeList node = Doc.SelectNodes("//BaiDang[ID='" + dto.ID + "']");
                   for (int i = 0;i<node.Count;i++ )
                    {
                        for (int j = 0; j < node[i].ChildNodes.Count; j++)
                        {
                            if (j == 1)
                                dto.NoiDung = node[i].ChildNodes[j].InnerText;
                        }
                    }
                       list.Add(dto);
                }
            }
            catch (Exception)
            {
                return list;
            }
            return list;
        }
        [HttpGet]
        public bool Post(string tuade, string ngaydang, string nguoidang, string noidung)
        {
            bool a = false;
            try
            {
                int maxID=-1;
                ngaydang = DateTime.Now.Day+"/"+ DateTime.Now.Month+"/"+ DateTime.Now.Year;
                string query = "SELECT MAX(ID) from BaiDang";
                DataTable TB = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in TB.Rows)
                {
                    maxID = Convert.ToInt16(dr[0]) + 1;
                }
                query = " insert into BaiDang Values('" + maxID.ToString() + "',N'" + tuade + "','" + ngaydang + "',N'" + nguoidang + "')";
                DataProvider.ExecuteQuery(query);
                XmlDocument Doc = new XmlDocument();
                Doc.Load(rootdata + "/NoiDungBaiDang.XML");

                XmlNode nodeLH = Doc.DocumentElement;
                XmlNode nodeSV = Doc.CreateElement("BaiDang");

                XmlNode ID = Doc.CreateElement("ID");
                ID.InnerText = maxID.ToString();
                XmlNode NoiDung = Doc.CreateElement("NoiDung");
                NoiDung.InnerText = noidung;

                nodeSV.AppendChild(ID);
                nodeSV.AppendChild(NoiDung);
                nodeLH.AppendChild(nodeSV);

                Doc.Save(rootdata + "/NoiDungBaiDang.XML");

                a = true;
            }
            catch (Exception ex)
            {
                return false;
            }
            return a;
        }
        [HttpGet]
        public string PhanQuyen(string type)
        {
            string pq = "";
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + type+ "']");
                if (node.Count <= 0)
                {

                }
                else
                {
                    pq = node[0].Attributes["Type"].Value;                  
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            return pq;
        }
        [HttpGet]
        public bool AccountFB(string name,string pass, string url,string yh)
        {
            bool A = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {

                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name + "']");
                #region update
                for (int i = 0; i < node.Count; i++)
                {
                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = name;
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = "0";
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = pass;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = url;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = "online";
                    XmlAttribute email = Doc.CreateAttribute("yh");
                    email.Value = node[i].Attributes["yh"].Value;

                    nodeSV.Attributes.Append(email);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);

                    for (int j = 0; j < node[0].ChildNodes.Count; j++)
                    {
                        XmlNode temp = Doc.CreateElement("Friend");
                        temp.InnerText = node[0].ChildNodes[j].InnerText;
                        nodeSV.AppendChild(temp);
                    }

                    node[i].ParentNode.ReplaceChild(nodeSV, node[i]);
                    A = true;
                }
                #endregion
                #region Add
                if (node.Count == 0)
                {
                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = name;
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = "0";
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = pass;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = url;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = "online";

                    XmlAttribute yh1 = Doc.CreateAttribute("yh");
                    yh1.Value = yh;

                    nodeSV.Attributes.Append(yh1);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);

                    nodeLH.AppendChild(nodeSV);
                }
                #endregion
                A = true;
                Doc.Save(rootdata + "/Database.XML");
            }
            catch (Exception ex)
            {
                return false;
            }
            return A;
        }
        [HttpGet]
        public List<TinTheThao> News(int i)
        {
            string Url = "http://dantri.com.vn/the-thao.htm";
            List<TinTheThao> list = new List<TinTheThao>();

            string news = "//div[@class='mr1']";
            HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
            HtmlAgilityPack.HtmlDocument doc = web.Load(Url);
            foreach (HtmlNode link in doc.DocumentNode.SelectNodes(news))
            {
                HtmlNode h1 = link.ChildNodes["h1"];
                HtmlNode h2 = link.ChildNodes["h2"];
                HtmlNode a;
                if (h1 == null)
                    a = h2.ChildNodes["a"];
                else
                    a = h1.ChildNodes["a"];
                HtmlNode div = link.ChildNodes["div"];

                TinTheThao tin = new TinTheThao();
                tin.tittle = a.Attributes["title"].Value;
                tin.url = "http://dantri.com.vn/the-thao" + a.Attributes["href"].Value;
                tin.content = div.InnerText;
                list.Add(tin);

            }

            return list;
        }
        [HttpGet]
        public bool LogOut(string name,int id)
        {
            bool a = false;
            
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name+ "']");
                if (node.Count <= 0)
                {

                }
                else
                {

                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = node[0].Attributes["UserName"].Value;
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = node[0].Attributes["Type"].Value;
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = node[0].Attributes["Pass"].Value;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = node[0].Attributes["Avatar"].Value;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = "offline";
                    XmlAttribute yh = Doc.CreateAttribute("yh");
                    yh.Value = node[0].Attributes["yh"].Value;

                    nodeSV.Attributes.Append(yh);
                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);
                    for (int j = 0; j < node[0].ChildNodes.Count; j++)
                    {
                        XmlNode temp = Doc.CreateElement("Friend");
                        temp.InnerText = node[0].ChildNodes[j].InnerText;
                        nodeSV.AppendChild(temp);
                    }
                    node[0].ParentNode.ReplaceChild(nodeSV, node[0]);
                    Doc.Save(rootdata + "/Database.XML");
                    a = true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return a;
        }
        [HttpGet]
        public string KTDangNhap(string name,string pass,string yh)
        {
            XmlDocument Doc = new XmlDocument();
            string Name = "";
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name + "' and @Pass='" + pass + "']");
                if (node.Count <= 0)
                {

                }
                else
                {
                    Name = node[0].Attributes["UserName"].Value;

                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = node[0].Attributes["UserName"].Value;
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = node[0].Attributes["Type"].Value;
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = node[0].Attributes["Pass"].Value;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = node[0].Attributes["Avatar"].Value;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = "online";
                    XmlAttribute yh1 = Doc.CreateAttribute("yh");
                    yh1.Value = "";

                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(yh1);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);
                    for (int j = 0; j < node[0].ChildNodes.Count; j++)
                    {
                        XmlNode temp = Doc.CreateElement("Friend");
                        temp.InnerText = node[0].ChildNodes[j].InnerText;
                        nodeSV.AppendChild(temp);
                    }
                    node[0].ParentNode.ReplaceChild(nodeSV, node[0]);
                    Doc.Save(rootdata + "/Database.XML");
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            return Name;
        }
        [HttpGet]
        public List<Users> GetListFriend(string username)
        {
            List<Users> list = new List<Users>();

            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + username +"']");
                if (node.Count <= 0)
                {

                }
                else
                {
                    string temp = "";
                    for (int i = 0; i < node[0].ChildNodes.Count; i++)
                    {
                        Users user = new Users();
                        user.Name = node[0].ChildNodes[i].InnerText;
                        XmlNodeList nodestt = Doc.SelectNodes("//User[@UserName='" + user.Name + "']");
                        user.stt = nodestt[0].Attributes["status"].Value;
                        user.avatar=nodestt[0].Attributes["Avatar"].Value;
                        list.Add(user); 
                    }
                }
            }
            catch (Exception ex)
            {
                return list;
            }
            return list;
        }
        [HttpGet]
        public string GetAvatar(string name,int id,int id1)
        {
            string a = "";
            XmlDocument Doc = new XmlDocument();
            string Name = "";
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name  + "']");
                if (node.Count <= 0)
                {

                }
                else
                {
                    
                    a = node[0].Attributes["Avatar"].Value;
                    
                }
            }
            catch (Exception ex)
            {
                return "";
            }
            return a;
        }
        [HttpGet]
        public List<Message> GetHistoryMessage(string name1,string name2)
        {
            List<Message> list = new List<Message>();
            List<Message> list1 = new List<Message>();
            List<Message> list2 = new List<Message>();
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/History.XML");
            try
            {
                XmlNodeList node1 = Doc.SelectNodes("//mes[@from='" + name1 + "' and @to='" + name2 + "']");
                if (node1.Count <= 0)
                {
                    return list;
                }
                else
                {
                    for (int i = 0; i<node1.Count; i++)
                    {
                        Message mes = new Message();
                        mes.from = node1[i].Attributes["from"].Value;
                        mes.to = node1[i].Attributes["to"].Value;
                        mes.content = node1[i].Attributes["content"].Value;
                        mes.stt = Convert.ToInt16( node1[i].Attributes["stt"].Value);
                        list1.Add(mes);
                    }
                }

                XmlNodeList node2 = Doc.SelectNodes("//mes[@from='" + name2 + "' and @to='" + name1 + "']");
                if (node2.Count <= 0)
                {
                    return list;
                }
                else
                {
                    for (int i = 0; i < node2.Count; i++)
                    {
                        Message mes = new Message();
                        mes.from = node2[i].Attributes["from"].Value;
                        mes.to = node2[i].Attributes["to"].Value;
                        mes.content = node2[i].Attributes["content"].Value;
                        mes.stt = Convert.ToInt16(node2[i].Attributes["stt"].Value);
                        list2.Add(mes);
                    }
                }
                int i1 = 0, i2 = 0;
                for (int i = 0; i < node1.Count + node2.Count; i++)
                {
                    if (i1 < list1.Count && i2 < list2.Count)
                    {
                        if (list1[i1].stt < list2[i2].stt)
                        {
                            list.Add(list1[i1]);
                            i1++;
                        }
                        else
                        {
                            list.Add(list2[i2]);
                            i2++;
                        }
                    }
                    else
                    {
                        if (i1 == list1.Count)
                        {
                            list.Add(list2[i2]);
                            i2++;
                        }
                        else
                        {
                            list.Add(list1[i1]);
                            i1++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return list;
            }
            return list;
        }
        [HttpGet]
        public bool DeleteFriend(string username,string name)
        {
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            bool Bool = false;
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + username+ "']");
                if (node.Count <= 0)
                {
                    return Bool;
                }
                else
                {
                    for (int i = 0; i < node[0].ChildNodes.Count; i++)
                    {
                        if (node[0].ChildNodes[i].InnerText == name)
                        {
                            Bool = true;
                            node[0].RemoveChild(node[0].ChildNodes[i]);
                            Doc.Save(rootdata + "/Database.XML");
                        }

                    }
                }
                XmlNodeList node1 = Doc.SelectNodes("//User[@UserName='" + name + "']");
                if (node1.Count <= 0)
                {
                    return Bool;
                }
                else
                {
                    for (int i = 0; i < node1[0].ChildNodes.Count; i++)
                    {
                        if (node1[0].ChildNodes[i].InnerText == username)
                        {
                            Bool = true;
                            node1[0].RemoveChild(node1[0].ChildNodes[i]);
                            Doc.Save(rootdata + "/Database.XML");
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            
            return Bool;
        }
        [HttpGet]
        public List<Message> ReceiveMessage(string name)
        {
            List<Message> list = new List<Message>();
            //
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Message.XML");
            try
            {
                MongoServerSettings settings = new MongoServerSettings();
                settings.Server = new MongoServerAddress("localhost", 27017);
                // Create server object to communicate with our server
                MongoServer server = new MongoServer(settings);
                // Get our database instance to reach collections and data
                var database = server.GetDatabase("DatabaseMongoDB");
                //var query = Query.Equals("To", name);
                var query = Query.EQ("To", name);
                var tt = database.GetCollection("Message").Find(query);
                foreach (var a in tt)
                {
                    Message mes = new Message();
                    mes.from = a["From"].AsString;
                    mes.to = a["To"].AsString;
                    mes.content = a["Content"].AsString;
                    list.Add(mes);
                    database.GetCollection("Message").Remove(query);
                }     
        
                //XmlNodeList node = Doc.SelectNodes("//mes[@to='" +name + "']");
                //if (node.Count <= 0)
                //{
                //    return list;
                //}
                //else
                //{
                //    for (int i = 0; i<node.Count; i++)
                //    {
                //        Message mes = new Message();
                //        mes.from = node[i].Attributes["from"].Value;
                //        mes.to = node[i].Attributes["to"].Value;
                //        mes.content=node[i].Attributes["content"].Value;
                //        node[i].ParentNode.RemoveChild(node[i]);
                //        list.Add(mes);
                //    }
                //}
                //Doc.Save(rootdata + "/Message.XML");
            }
            catch (Exception ex)
            {
                return list;
            }
            return list;
        }
        [HttpGet]
        public bool AddUser(string Name,string pass,int id,string url,int type)
        {
            
            bool Bool = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" +Name + "']");
                if (node.Count == 0)
                {
                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = Name;
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = pass;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = url;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = "offline";
                    XmlAttribute yh= Doc.CreateAttribute("yh");
                    yh.Value = "";
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = type.ToString();

                    nodeSV.Attributes.Append(yh);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);

                    nodeLH.AppendChild(nodeSV);

                    Doc.Save(rootdata + "/Database.XML");
                    Bool = true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return Bool;
        }
        [HttpGet]
        public bool AddFriend(string user,string name)
        {
            bool B = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {
                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + user + "']");
                if (node.Count != 0)
                {
                    XmlNodeList nodeF=Doc.SelectNodes("//User[@UserName='" + name + "']");
                    int a = 0;
                    if (nodeF.Count != 0)
                    {
                        for (int i = 0; i < node[0].ChildNodes.Count; i++)
                        {
                            if (node[0].ChildNodes[i].InnerText == name)
                            {
                                a = 1;
                                break;
                            };
                        }
                    }
                    if (nodeF.Count != 0 && a==0)
                    {
                        XmlNode nodeLH = Doc.DocumentElement;
                        XmlNode nodeSV = Doc.CreateElement("User");

                        XmlNode Friend = Doc.CreateElement("Friend");
                        Friend.InnerText = name;
                        node[0].AppendChild(Friend);

                        XmlNode Friend1 = Doc.CreateElement("Friend");
                        Friend1.InnerText = user;
                        nodeF[0].AppendChild(Friend1);
                        Doc.Save(rootdata + "/Database.XML");
                        B = true;
                    }
                    
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return B;
        }
        [HttpGet]
        public bool SendMessage(string from,string to,string content)
        {
            bool a = false;
            XmlDocument Doc = new XmlDocument();
            XmlDocument Doc1 = new XmlDocument();
            Doc.Load(rootdata + "/Message.XML");
            Doc1.Load(rootdata + "/History.XML");
            try
            {
                Message msg = new Message();
                //XmlNode nodeLH = Doc.DocumentElement;
                XmlNode nodeLH1 = Doc1.DocumentElement;

                //XmlNode nodeSV = Doc.CreateElement("mes");
                //XmlNode nodeSV1 = Doc1.CreateElement("mes");

                //XmlAttribute HT = Doc.CreateAttribute("from");
                //HT.Value = from;
                msg.from = from;
                //XmlAttribute Pa = Doc.CreateAttribute("to");
                //Pa.Value = to;
                msg.to = to;
                //XmlAttribute Av = Doc.CreateAttribute("content");
                //Av.Value = content;
                msg.content = content;
                //XmlAttribute HT1 = Doc1.CreateAttribute("from");
                //HT1.Value = from;
                //XmlAttribute Pa1 = Doc1.CreateAttribute("to");
                //Pa1.Value = to;
                //XmlAttribute Av1 = Doc1.CreateAttribute("content");
                //Av1.Value = content;
                XmlAttribute stt = Doc1.CreateAttribute("stt");
                stt.Value = (nodeLH1.ChildNodes.Count + 1).ToString();
                msg.stt =Convert.ToInt16( stt.Value);

                //nodeSV.Attributes.Append(HT);
                //nodeSV.Attributes.Append(Pa);
                //nodeSV.Attributes.Append(Av);

                //nodeSV1.Attributes.Append(HT1);
                //nodeSV1.Attributes.Append(Pa1);
                //nodeSV1.Attributes.Append(Av1);
                //nodeSV1.Attributes.Append(stt);

                //nodeLH.AppendChild(nodeSV);
                //nodeLH1.AppendChild(nodeSV1);

                //Doc.Save(rootdata + "/Message.XML");
                //Doc1.Save(rootdata + "/History.XML");
                MGDB(msg);
                a = true;
            }
            catch (Exception ex)
            {
                return false;
            }
            return a;
        }
        [HttpGet]
        public bool UpdateAvatar(string name,string url)
        {
            bool b = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {

                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name  + "']");
                for (int i = 0; i < node.Count; i++)
                {
                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = node[0].Attributes["UserName"].Value;
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = node[0].Attributes["Type"].Value;
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = node[0].Attributes["Pass"].Value;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = url;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = node[i].Attributes["status"].Value;
                    XmlAttribute yh= Doc.CreateAttribute("yh");
                    yh.Value = node[i].Attributes["yh"].Value;

                    nodeSV.Attributes.Append(yh);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);

                    for (int j = 0; j < node[0].ChildNodes.Count; j++)
                    {
                        XmlNode temp = Doc.CreateElement("Friend");
                        temp.InnerText = node[0].ChildNodes[j].InnerText;
                        nodeSV.AppendChild(temp);
                    }

                    node[i].ParentNode.ReplaceChild(nodeSV, node[i]);
                    b = true;
                }

                Doc.Save(rootdata + "/Database.XML");
            }
            catch (Exception ex)
            {
                return false;
            }
            return b;
           
        }
        [HttpGet]
        public bool UpdateUser(string pass,string name,string newpass)
        {
            bool B = false;
            XmlDocument Doc = new XmlDocument();
            Doc.Load(rootdata + "/Database.XML");
            try
            {

                XmlNodeList node = Doc.SelectNodes("//User[@UserName='" + name + "' and @Pass='" + pass + "']");
                for (int i = 0; i < node.Count; i++)
                {
                    XmlNode nodeLH = Doc.DocumentElement;
                    XmlNode nodeSV = Doc.CreateElement("User");

                    XmlAttribute HT = Doc.CreateAttribute("UserName");
                    HT.Value = node[0].Attributes["UserName"].Value;
                    XmlAttribute Type = Doc.CreateAttribute("Type");
                    Type.Value = node[0].Attributes["Type"].Value;
                    XmlAttribute Pa = Doc.CreateAttribute("Pass");
                    Pa.Value = newpass;
                    XmlAttribute Av = Doc.CreateAttribute("Avatar");
                    Av.Value = node[0].Attributes["Avatar"].Value;
                    XmlAttribute stt = Doc.CreateAttribute("status");
                    stt.Value = node[i].Attributes["status"].Value;
                    XmlAttribute yh = Doc.CreateAttribute("yh");
                    yh.Value = node[i].Attributes["yh"].Value;

                    nodeSV.Attributes.Append(yh);
                    nodeSV.Attributes.Append(Type);
                    nodeSV.Attributes.Append(HT);
                    nodeSV.Attributes.Append(Pa);
                    nodeSV.Attributes.Append(Av);
                    nodeSV.Attributes.Append(stt);

                    for (int j = 0; j< node[0].ChildNodes.Count; j++)
                    {
                        XmlNode temp = Doc.CreateElement("Friend") ;
                        temp.InnerText=node[0].ChildNodes[j].InnerText;
                        nodeSV.AppendChild(temp);
                    }

                    node[i].ParentNode.ReplaceChild(nodeSV, node[i]);
                    B = true;
                }

                Doc.Save(rootdata + "/Database.XML");
            }
            catch (Exception ex)
            {
                return false;
            }
            return B;
        }
       
        public void MGDB(Message msg)
        {
            try
            {
                XmlDocument Doc1 = new XmlDocument();
                Doc1.Load(rootdata + "/History.XML");
                XmlNode nodeLH1 = Doc1.DocumentElement;
                XmlNode nodeSV1 = Doc1.CreateElement("mes");
                XmlAttribute HT1 = Doc1.CreateAttribute("from");
                HT1.Value = msg.from;
                XmlAttribute Pa1 = Doc1.CreateAttribute("to");
                Pa1.Value = msg.to;
                XmlAttribute Av1 = Doc1.CreateAttribute("content");
                Av1.Value = msg.content;
                XmlAttribute stt = Doc1.CreateAttribute("stt");
                stt.Value = msg.stt.ToString();
                nodeSV1.Attributes.Append(HT1);
                nodeSV1.Attributes.Append(Pa1);
                nodeSV1.Attributes.Append(Av1);
                nodeSV1.Attributes.Append(stt);
                nodeLH1.AppendChild(nodeSV1);
                Doc1.Save(rootdata + "/History.XML");

                MongoServerSettings settings = new MongoServerSettings();
                settings.Server = new MongoServerAddress("localhost", 27017);
                // Create server object to communicate with our server
                MongoServer server = new MongoServer(settings);
                // Get our database instance to reach collections and data
                var database = server.GetDatabase("DatabaseMongoDB");

                var Data = database.GetCollection("Message");

                //// Create BsonDocument object for new user
                var Mes = new BsonDocument();
                Mes["From"] = msg.from;
                Mes["To"] = msg.to;
                Mes["Content"] = msg.content;
                Mes["STT"] = msg.stt;

                // Insert new user object to collection
                Data.Insert(Mes);
            }
            catch (Exception ex)
            {
            }
        }
        [HttpGet]
        public List<FlightInfo> ListInfo(int direction, string from, string to, string dayfrom, string dayto)
        {

            List<FlightInfo> list = new List<FlightInfo>();
            List<FlightInfo> listreturn = new List<FlightInfo>();
            string AirPortFrom = "";
            string AirPortTo = "";
            string timefrom = null;
            string timeto = null;
            #region SanBay
            switch (from)
            {
                case "BMW":
                    AirPortFrom = "Buôn Ma Thuột";
                    break;
                case "CAH":
                    AirPortFrom = "Cà Mau";
                    break;
                case "VCA":
                    AirPortFrom = "Cần Thơ";
                    break;
                case "HUI":
                    AirPortFrom = "Huế";
                    break;
                case "HAN":
                    AirPortFrom = "Hà Nội";
                    break;
                case "HPH":
                    AirPortFrom = "Hải Phòng";
                    break;
                case "NHA":
                    AirPortFrom = "Nha Trang";
                    break;
                case "PQC":
                    AirPortFrom = "Phú Quốc";
                    break;
                case "PXU":
                    AirPortFrom = "Pley Ku";
                    break;
                case "UIH":
                    AirPortFrom = "Qui Nhơn";
                    break;
                case "VKG":
                    AirPortFrom = "Rạch Giá";
                    break;
                case "VCL":
                    AirPortFrom = "Tam Kỳ";
                    break;
                case "SGN":
                    AirPortFrom = "Hồ Chí Minh";
                    break;
                case "TBB":
                    AirPortFrom = "Tuy Hòa";
                    break;
                case "VII":
                    AirPortFrom = "Vinh";
                    break;
                case "DIN":
                    AirPortFrom = "Điện Biên";
                    break;
                case "DLI":
                    AirPortFrom = "Đà Lạt";
                    break;
                case "DAD":
                    AirPortFrom = "Đà nẵng";
                    break;
                case "VDH":
                    AirPortFrom = "Đồng Hới";
                    break;

            }
            switch (to)
            {
                case "BMW":
                    AirPortTo = "Buôn Ma Thuột";
                    break;
                case "CAH":
                    AirPortTo = "Cà Mau";
                    break;
                case "VCA":
                    AirPortTo = "Cần Thơ";
                    break;
                case "HUI":
                    AirPortTo = "Huế";
                    break;
                case "HAN":
                    AirPortTo = "Hà Nội";
                    break;
                case "HPH":
                    AirPortTo = "Hải Phòng";
                    break;
                case "NHA":
                    AirPortTo = "Nha Trang";
                    break;
                case "PQC":
                    AirPortTo = "Phú Quốc";
                    break;
                case "PXU":
                    AirPortTo = "Pley Ku";
                    break;
                case "UIH":
                    AirPortTo = "Qui Nhơn";
                    break;
                case "VKG":
                    AirPortTo = "Rạch Giá";
                    break;
                case "VCL":
                    AirPortTo = "Tam Kỳ";
                    break;
                case "SGN":
                    AirPortTo = "Hồ Chí Minh";
                    break;
                case "TBB":
                    AirPortTo = "Tuy Hòa";
                    break;
                case "VII":
                    AirPortTo = "Vinh";
                    break;
                case "DIN":
                    AirPortTo = "Điện Biên";
                    break;
                case "DLI":
                    AirPortTo = "Đà Lạt";
                    break;
                case "DAD":
                    AirPortTo = "Đà nẵng";
                    break;
                case "VDH":
                    AirPortTo = "Đồng Hới";
                    break;

            }
            #endregion
            try
            {
                FlightInfo info = new FlightInfo();
                List<string> fee = new List<string>();
                // direction=1 - 1chieu, direction=2 - 2chieu
                #region Mekong

                try
                {

                    string dic1 = "";
                    if (direction == 1)
                        dic1 = "OneWay";
                    else
                        dic1 = "RoundTrip";
                    string[] mktemp = dayfrom.Split('/');
                    string mkdayfrom = mktemp[0];
                    string mkmonthfrom = mktemp[2] + "-" + mktemp[1];
                    string mkdayto = "";
                    string mkmonthto = "";
                    if (dayto != null)
                    {
                        mktemp = dayto.Split('/');
                        mkdayto = mktemp[0];
                        mkmonthto = mktemp[2] + "-" + mktemp[1];
                    }

                    string MKurl =

               "__EVENTTARGET=" +
                "&__EVENTARGUMENT=" +
                "&__VIEWSTATE=/wEPDwUBMGRk77h9CNekodLMZhdJu3riPx9ebN8=" +
                "&pageToken=" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$RadioButtonMarketStructure=" + dic1 +
                "&originStation1=HAN" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$TextBoxMarketOrigin1=" + from +
                "&destinationStation1=SGN" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$TextBoxMarketDestination1=" + to +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketDay1=2" + mkdayfrom +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketMonth1=" + mkmonthfrom +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketDateRange1=0|4" +
                "&originStation2=" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$TextBoxMarketOrigin2=Điểm đi..." +
                "&destinationStation2=" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$TextBoxMarketDestination2=Điểm đến..." +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketDay2=" + mkdayto +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketMonth2=" + mkmonthto +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListMarketDateRange2=0|4" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListPassengerType_ADT=1" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListPassengerType_CHD=0" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListPassengerType_INFANT=0" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$DropDownListSearchBy=columnView" +
                "&ControlGroupSearchInfoView$AvailabilitySearchInputSearchInfoView$ButtonSubmit=Tìm chuyến bay";

                    byte[] postBytes = Encoding.UTF8.GetBytes(MKurl);

                    HttpWebRequest MKwebRequest = (HttpWebRequest)WebRequest.Create(" https://booking.airmekong.com.vn/SearchInfo.aspx");
                    MKwebRequest.Method = "POST";
                    CookieContainer a = new CookieContainer();

                    MKwebRequest.CookieContainer = a;
                    MKwebRequest.ContentType = "application/x-www-form-urlencoded";
                    MKwebRequest.ContentLength = postBytes.Length;

                    Stream MKpostStream = MKwebRequest.GetRequestStream();
                    MKpostStream.Write(postBytes, 0, postBytes.Length);
                    MKpostStream.Close();

                    HttpWebResponse MKwebResponse = (HttpWebResponse)MKwebRequest.GetResponse();

                    Console.WriteLine(MKwebResponse.StatusCode);
                    Console.WriteLine(MKwebResponse.Server);

                    Stream MKresponseStream = MKwebResponse.GetResponseStream();
                    StreamReader MKresponseStreamReader = new StreamReader(MKresponseStream, Encoding.UTF8);
                    string MKresult = MKresponseStreamReader.ReadToEnd();

                    String MKsDiskFile = HttpContext.Current.Server.MapPath("~/result.htm");
                    //string root = HttpContext.Current.Server.MapPath;
                    StreamWriter MKoWriter = new StreamWriter(MKsDiskFile);
                    MKoWriter.Write(MKresult);

                    MKoWriter.Close();
                    MKresponseStreamReader.Close();
                    MKwebResponse.Close();

                    HtmlAgilityPack.HtmlWeb webMK = new HtmlWeb();
                    HtmlAgilityPack.HtmlDocument docMK = webMK.Load(HttpContext.Current.Server.MapPath("~/result.htm"));
                    string MKpath = "//table[@id='availabilityTable0']";
                    foreach (HtmlNode link in docMK.DocumentNode.SelectNodes(MKpath))
                    {

                        foreach (HtmlNode link1 in link.SelectNodes(link.XPath + "/tr[td[@class='left footnote']]"))
                        {
                            FlightInfo MKinfo = new FlightInfo();
                            MKinfo.From = AirPortFrom + "  (" + from + ")";
                            MKinfo.To = AirPortTo + "  (" + to + ")";
                            MKinfo.Hang = "Mekong Air";
                            MKinfo.Image = "http://nammekong.vn/UserFiles/Image/logo_airmekong.png.jpeg";
                            string[] temp;
                            HtmlNode MKTimeFrom = link1.SelectSingleNode(link1.XPath + "/td[2]");
                            temp = MKTimeFrom.InnerText.Split(' ');
                            MKinfo.TimeFrom = temp[0];
                            HtmlNode MKTimeTo = link1.SelectSingleNode(link1.XPath + "/td[3]");
                            temp = MKTimeTo.InnerText.Split(' ');

                            MKinfo.TimeTo = temp[0];
                            HtmlNode MKfee1 = link1.SelectSingleNode(link1.XPath + "/td[8]/p[1]");
                            if (MKfee1 == null)
                            {
                                HtmlNode MKfee2 = link1.SelectSingleNode(link1.XPath + "/td[7]/p[1]");
                                if (MKfee2 == null)
                                {
                                    HtmlNode MKfee3 = link1.SelectSingleNode(link1.XPath + "/td[6]/p[1]");
                                    if (MKfee3 == null)
                                    {
                                        HtmlNode MKfee4 = link1.SelectSingleNode(link1.XPath + "/td[5]/p[1]");
                                        if (MKfee4 == null)
                                        {
                                            HtmlNode MKfee5 = link1.SelectSingleNode(link1.XPath + "/td[4]/p[1]");
                                            if (MKfee5 != null)
                                            {
                                                MKinfo.GiaTien = MKfee5.InnerText + "VND";
                                                list.Add(MKinfo);
                                            }
                                        }
                                        else
                                        {
                                            MKinfo.GiaTien = MKfee4.InnerText + "VND";
                                            list.Add(MKinfo);
                                        }
                                    }
                                    else
                                    {
                                        MKinfo.GiaTien = MKfee3.InnerText + "VND";
                                        list.Add(MKinfo);
                                    }
                                }
                                else
                                {
                                    MKinfo.GiaTien = MKfee2.InnerText + "VND";
                                    list.Add(MKinfo);
                                }
                            }
                            else
                            {
                                MKinfo.GiaTien = MKfee1.InnerText + "VND";
                                list.Add(MKinfo);
                            }
                        }

                    }
                    MKpath = "//table[@id='availabilityTable1']";
                    foreach (HtmlNode link in docMK.DocumentNode.SelectNodes(MKpath))
                    {

                        foreach (HtmlNode link1 in link.SelectNodes(link.XPath + "/tr[td[@class='left footnote']]"))
                        {
                            FlightInfo MKinfo = new FlightInfo();
                            MKinfo.From = AirPortTo + "  (" + to + ")";
                            MKinfo.To = AirPortFrom + "  (" + from + ")";
                            MKinfo.Hang = "Mekong Air";
                            MKinfo.Image = "http://nammekong.vn/UserFiles/Image/logo_airmekong.png.jpeg";
                            string[] temp;
                            HtmlNode MKTimeFrom = link1.SelectSingleNode(link1.XPath + "/td[2]");
                            temp = MKTimeFrom.InnerText.Split(' ');
                            MKinfo.TimeFrom = temp[0];
                            HtmlNode MKTimeTo = link1.SelectSingleNode(link1.XPath + "/td[3]");
                            temp = MKTimeTo.InnerText.Split(' ');
                            MKinfo.TimeTo = temp[0];
                            HtmlNode MKfee1 = link1.SelectSingleNode(link1.XPath + "/td[8]/p[1]");
                            if (MKfee1 == null)
                            {
                                HtmlNode MKfee2 = link1.SelectSingleNode(link1.XPath + "/td[7]/p[1]");
                                if (MKfee2 == null)
                                {
                                    HtmlNode MKfee3 = link1.SelectSingleNode(link1.XPath + "/td[6]/p[1]");
                                    if (MKfee3 == null)
                                    {
                                        HtmlNode MKfee4 = link1.SelectSingleNode(link1.XPath + "/td[5]/p[1]");
                                        if (MKfee4 == null)
                                        {
                                            HtmlNode MKfee5 = link1.SelectSingleNode(link1.XPath + "/td[4]/p[1]");
                                            if (MKfee5 != null)
                                            {
                                                MKinfo.GiaTien = MKfee5.InnerText + "VND";
                                                list.Add(MKinfo);
                                            }
                                        }
                                        else
                                        {
                                            MKinfo.GiaTien = MKfee4.InnerText + "VND";
                                            list.Add(MKinfo);
                                        }
                                    }
                                    else
                                    {
                                        MKinfo.GiaTien = MKfee3.InnerText + "VND";
                                        list.Add(MKinfo);
                                    }
                                }
                                else
                                {
                                    MKinfo.GiaTien = MKfee2.InnerText + "VND";
                                    list.Add(MKinfo);
                                }
                            }
                            else
                            {
                                MKinfo.GiaTien = MKfee1.InnerText + "VND";
                                list.Add(MKinfo);
                            }
                        }

                    }
                    if (true)
                    {
                        int aaa = 2;
                    }
                }
                catch (Exception ex)
                {

                }
                #endregion
                #region Jet
                string dic = "";
                if (direction == 1)
                    dic = "OneWay";
                else
                    dic = "RoundTrip";
                string Url1 =
        "&__EVENTARGUMENT=" +
        "&__VIEWSTATE=/wEPDwUBMGQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgEFJ01lbWJlckxvZ2luU2VhcmNoVmlldyRtZW1iZXJfUmVtZW1iZXJtZTxtWS/I2BXFBfalk96y3LBuGXXD" +
        "&pageToken=" +
        "&total_price=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$RadioButtonMarketStructure=" + dic +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin1=(" + from + ")" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination1=(" + to + ")" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate1=" + dayfrom +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate1=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListCurrency=VND" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin2=(" + to + ")" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination2=(" + from + ")" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate2=" + dayto +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate2=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin3=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination3=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate3=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate3=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin4=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination4=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate4=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate4=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin5=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination5=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate5=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate5=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketOrigin6=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextBoxMarketDestination6=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDepartureDate6=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$TextboxDestinationDate6=" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_ADT=1" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_CHD=0" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$DropDownListPassengerType_INFANT=0" +
        "&ControlGroupSearchView$AvailabilitySearchInputSearchView$ButtonSubmit=";


                try
                {

                    byte[] postBytes = Encoding.UTF8.GetBytes(Url1);

                    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://booknow.jetstar.com/Search.aspx?culture=vi-VN");
                    webRequest.Method = "POST";
                    CookieContainer a = new CookieContainer();

                    webRequest.CookieContainer = a;
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.ContentLength = postBytes.Length;

                    Stream postStream = webRequest.GetRequestStream();
                    postStream.Write(postBytes, 0, postBytes.Length);
                    postStream.Close();

                    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

                    Console.WriteLine(webResponse.StatusCode);
                    Console.WriteLine(webResponse.Server);

                    Stream responseStream = webResponse.GetResponseStream();
                    StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8);
                    string result = responseStreamReader.ReadToEnd();

                    String sDiskFile = HttpContext.Current.Server.MapPath("~/result.htm");
                    //string root = HttpContext.Current.Server.MapPath;
                    StreamWriter oWriter = new StreamWriter(sDiskFile);
                    oWriter.Write(result);

                    oWriter.Close();
                    responseStreamReader.Close();
                    webResponse.Close();

                    HtmlAgilityPack.HtmlWeb webjet = new HtmlWeb();
                    HtmlAgilityPack.HtmlDocument docJet = webjet.Load(HttpContext.Current.Server.MapPath("~/result.htm"));
                    string XpathJet = "//tbody/tr[not(@*)]";
                    foreach (HtmlNode link in docJet.DocumentNode.SelectNodes(XpathJet))
                    {
                        FlightInfo JetInfo = new FlightInfo();
                        JetInfo.Hang = "Jet Star";
                        string t;
                        HtmlNode JetTimeFrom = link.SelectSingleNode(link.XPath + "/td[1]/strong[1]");
                        if (JetTimeFrom != null)
                        {
                            t = JetTimeFrom.InnerText;
                            JetInfo.TimeFrom = t;
                            JetInfo.Image = "http://timsanpham.com/images/pictures/18763_20080911112741_jetstar_logo[1]_.png";
                            HtmlNode JetFrom = link.SelectSingleNode(link.XPath + "/td[1]/a[1]");
                            t = JetFrom.InnerText;
                            if (t.Contains(AirPortFrom))
                            {
                                t = AirPortFrom + " (" + from + ")";
                            }
                            if (t.Contains(AirPortTo))
                            {
                                t = AirPortTo + " (" + to + ")";
                            }
                            JetInfo.From = t;
                            HtmlNode JetTo = link.SelectSingleNode(link.XPath + "/td[2]/a[1]");
                            t = JetTo.InnerText;
                            if (t.Contains(AirPortFrom))
                            {
                                t = AirPortFrom + " (" + from + ")";
                            }
                            if (t.Contains(AirPortTo))
                            {
                                t = AirPortTo + " (" + to + ")";
                            }
                            JetInfo.To = t;
                            HtmlNode Jtemp = link.SelectSingleNode(link.XPath + "/td[1]/a[1]");
                            HtmlNode JetTimeTo = link.SelectSingleNode(link.XPath + "/td[2]/strong[1]");
                            t = JetTimeTo.InnerText;
                            JetInfo.TimeTo = t;
                            HtmlNode JetFee = link.SelectSingleNode(link.XPath + "/td[4]/div[1]/label[1]");
                            if (JetFee != null)
                            {
                                t = JetFee.InnerText;
                                t = t.Remove(0, 3) + "VND";
                                JetInfo.GiaTien = t;
                                list.Add(JetInfo);
                            }
                            else
                            {

                            }

                        }
                    }
                }
                catch (Exception ex)
                {

                }



                #endregion

                #region VN Airlines
                if (direction == 1)
                    dic = "onewaytravel";
                else
                    dic = "returntravel";

                string[] datetimefrom = dayfrom.Split('/');
                string ngayfrom = datetimefrom[0];
                int so = Convert.ToInt32(ngayfrom);
                ngayfrom = so.ToString();
                string monthfrom = "";
                monthfrom = datetimefrom[1];
                switch (monthfrom)
                {
                    case "01":
                        monthfrom = "JAN";
                        break;
                    case "02":
                        monthfrom = "FEB";
                        break;
                    case "03":
                        monthfrom = "MAR";
                        break;
                    case "04":
                        monthfrom = "APR";
                        break;
                    case "05":
                        monthfrom = "MAY";
                        break;
                    case "06":
                        monthfrom = "JUN";
                        break;
                    case "07":
                        monthfrom = "JUL";
                        break;
                    case "08":
                        monthfrom = "AUG";
                        break;
                    case "09":
                        monthfrom = "SEP";
                        break;
                    case "10":
                        monthfrom = "OCT";
                        break;
                    case "11":
                        monthfrom = "NOV";
                        break;
                    case "12":
                        monthfrom = "DEC";
                        break;
                }
                string monthto = "";
                string ngayto = "";
                if (dayto != null)
                {
                    string[] datetimeto = dayto.Split('/');
                    ngayto = datetimeto[0];
                    so = Convert.ToInt32(ngayto);
                    ngayto = so.ToString();
                    monthto = "";
                    monthto = datetimeto[1];
                    switch (monthto)
                    {
                        case "01":
                            monthto = "JAN";
                            break;
                        case "02":
                            monthto = "FEB";
                            break;
                        case "03":
                            monthto = "MAR";
                            break;
                        case "04":
                            monthto = "APR";
                            break;
                        case "05":
                            monthto = "MAY";
                            break;
                        case "06":
                            monthto = "JUN";
                            break;
                        case "07":
                            monthto = "JUL";
                            break;
                        case "08":
                            monthto = "AUG";
                            break;
                        case "09":
                            monthto = "SEP";
                            break;
                        case "10":
                            monthto = "OCT";
                            break;
                        case "11":
                            monthto = "NOV";
                            break;
                        case "12":
                            monthto = "DEC";
                            break;
                    }
                }
                string Url = "https://cat.sabresonicweb.com/SSWVN/meridia?language=vi&posid=B3QE&page=requestAirMessage_air&action=airRequest&realrequestAir=realRequestAir&actionType=nonFlex&classService=CoachClass&currency=VND&"
                + "depTime=anytimeFromHost"
                + "&retTime=anytimeFromHost"
                + "&direction=" + dic
                + "&departCity=" + from
                + "&depDay=" + ngayfrom
                + "&depMonth=" + monthfrom
                + "&temp_date="
                + "&returnCity=" + to
                + "&retDay=" + ngayto
                + "&retMonth=" + monthto
                + "&temp_date=&ADT=1&CHD=0&INF=0&submit=T%C3%ACm%20chuy%E1%BA%BFn%20bay&wpf_timed_action_0VNAirline_00215IBEFastTabV2_00215bookingYourTrip_00515vn_0051513b92417f90_005158e04_1=&ShowNote=Qu%C3%BD%20kh%C3%A1ch%20c%E1%BA%A7%20nh%E1%BA%AD%20%C4%91u7847%20y%20%C4%91u7911%20%20th%C3%B4ng%20tin%20%C4%91u7875%20%20l%C3%A0m%20th%E1%BB%A7t%E1%BB%A5%20!&wclang=VI&txtFamilyName=&txtMidName=&slDeparture=&txtConfirmCode=&txtFlightNumber=&rdoDay=on&";
                HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(Url);


                int count = 0;
                string newpath = "//tr[@class='_out' or @class='_in']";
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes(newpath))
                {
                    string x = link.XPath;
                    HtmlNodeCollection flag = link.SelectNodes(x + "/td[div[@class='hoverTaxes']]");
                    foreach (HtmlNode temp in flag)
                    {
                        string xx = temp.XPath;
                        HtmlNode a = temp.SelectSingleNode(xx + "/div[@class='hoverTaxes']/table[1]/tr[4]/td[2]");
                        if (a == null)
                        {
                            fee.Add("Không Có");

                        }
                        else
                        {
                            string t = a.InnerText;
                            t = t.Replace("\r", "");
                            t = t.Replace("\n", "");
                            t = t.Replace("\t", "");
                            t = t.Replace(" ", "");
                            fee.Add(t);

                        }
                        count++;
                    }
                    if (count < 5)
                    {
                        for (int j = 0; j < 5 - count; j++)
                        {
                            fee.Add("Không Có");
                        }
                        count = 0;
                    }
                    else
                        count = 0;
                }

                count = 0;
                string path = "//td[@class='matrix_cal_body matrix_cal_body_dep' or " +
                "@class='matrix_cal_body matrix_cal_body_arr']";
                foreach (HtmlNode link1 in doc.DocumentNode.SelectNodes(path))
                {
                    string Xpath = link1.XPath;
                    string t;
                    if (link1.Attributes["class"].Value == "matrix_cal_body matrix_cal_body_dep")
                    {
                        #region
                        HtmlNode temp = link1.SelectSingleNode(Xpath + "/div[1]/div[2]/table[1]/tr[1]/td[1]");
                        t = temp.InnerText;
                        t = t.Replace("\r", "");
                        t = t.Replace("\n", "");
                        t = t.Replace("\t", "");
                        t = t.Replace(" ", "");
                        if (t.Contains(from))
                        {
                            t = AirPortFrom + " (" + from + ")";
                        }
                        if (t.Contains(to))
                        {
                            t = AirPortTo + " (" + to + ")"; ;
                        }
                        info.From = t;
                        temp = link1.SelectSingleNode(Xpath + "/div[1]/p");
                        info.TimeFrom = temp.InnerText;
                        #endregion

                    }
                    if (link1.Attributes["class"].Value == "matrix_cal_body matrix_cal_body_arr")
                    {
                        #region
                        HtmlNode temp = link1.SelectSingleNode(Xpath + "/div[1]/div[2]/table[1]/tr[1]/td[1]");
                        t = temp.InnerText;
                        t = t.Replace("\r", "");
                        t = t.Replace("\n", "");
                        t = t.Replace("\t", "");
                        t = t.Replace(" ", "");
                        if (t.Contains(to))
                        {
                            t = AirPortTo + " (" + to + ")"; ;
                        }
                        if (t.Contains(from))
                        {
                            t = AirPortFrom + " (" + from + ")";
                        }
                        info.To = t;
                        temp = link1.SelectSingleNode(Xpath + "/div[1]/p");
                        info.TimeTo = temp.InnerText;

                        info.Image = "https://cat.sabresonicweb.com/SSWVN/application/images/B3QE/airplane.gif";
                        for (int flag = count * 5 + 4; flag >= count * 5; flag--)
                        {
                            if (fee[flag] != "Không Có")
                            {
                                info.GiaTien = fee[flag];
                                break;
                            }
                        }
                        info.Hang = "VietName Airlines";
                        list.Add(info);

                        info = new FlightInfo();

                        count++;
                        #endregion
                    }

                }
                #endregion

                #region select flight
                for (int b = 0; b < list.Count; b++)
                {
                    if (timefrom != null && timeto != null)
                    {
                        bool a = list[b].From.Contains(from);
                        if ((list[b].From.Contains(from) && list[b].TimeFrom == timefrom) || (list[b].From.Contains(to) && list[b].TimeFrom == timeto))
                        {
                            listreturn.Add(list[b]);
                        }
                    }
                    else
                    {
                        if (timefrom == null && timeto != null)
                        {
                            if (list[b].From.Contains(from) || (list[b].From.Contains(to) && list[b].TimeFrom == timeto))
                            {
                                listreturn.Add(list[b]);
                            }
                        }
                        if (timefrom != null && timeto == null)
                        {
                            if ((list[b].From.Contains(from) && list[b].TimeFrom == timefrom) || list[b].From.Contains(to))
                            {
                                listreturn.Add(list[b]);
                            }
                        }
                        if (timefrom == null && timeto == null)
                        {
                            if (list[b].From.Contains(from) || list[b].From.Contains(to))
                            {
                                listreturn.Add(list[b]);
                            }
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                return listreturn;
            }
            return listreturn;
        }
        [HttpGet]
        public string PhuongTrang(string PhuongTrang)
        {
            List<BusInfo> list = new List<BusInfo>();
            string a = "";
            try
            {
                
                string Url = "http://www.futabuslines.com.vn/Default.aspx?tbid=9";
                HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(Url);
               // string xpath = "//td[@class='News_Default_Right']";
                string xpath = "//td[@class='CW_610']";
                
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes(xpath))
                {
                    a = link.InnerHtml;
                   
                    break;
                    #region PhuongTrang
                    // string newXpath = link.XPath;
                    //HtmlNodeCollection node = link.SelectNodes(newXpath + "/table[1]/tr");
                    //  node.Remove(0);
                    //int i = 1;
                    //foreach (HtmlNode temp in node)
                    //{
                    //    if (i != 1)
                    //    {
                    //        BusInfo info = new BusInfo();
                    //        HtmlNode nodetemp;
                    //        string text;
                    //        nodetemp = temp.SelectSingleNode(temp.XPath + "/td[1]");
                    //        text = xulichuoi(nodetemp.InnerText);
                    //        info.BenDi = text;
                    //        nodetemp = temp.SelectSingleNode(temp.XPath + "/td[2]");
                    //        text = xulichuoi(nodetemp.InnerText);
                    //        info.BenDen = text;
                    //        nodetemp = temp.SelectSingleNode(temp.XPath + "/td[3]");
                    //        text = xulichuoi(nodetemp.InnerText);
                    //        info.Loai = text;
                    //        nodetemp = temp.SelectSingleNode(temp.XPath + "/td[4]");
                    //        text = xulichuoi(nodetemp.InnerText);
                    //        info.Gia = text;
                    //        info.Hang = "Phương Trang";
                    //        info.Logo = "http://www.vinhhienvts.vn/sites/default/files/resize/userfiles/Logo_Phuong_Trang-540x588.png";
                    //        list.Add(info);
                    //    }
                    //    i++;
                    //}
                #endregion
                }
                
            }
            catch (Exception ex)
            {
                return a;
            }
            return a;
        }
        public string xulichuoi(string t)
        {
            t = t.Replace("\r", "");
            t = t.Replace("\n", "");
            t = t.Trim();
            return t;
        }
        [HttpGet]
        public List<string> MaiLinh(string MaiLinh)
        {
            List<string> list = new List<string>();
            try
            {
                
                string Nam = "http://www.mailinhexpress.vn/index.php?option=com_content&view=article&id=129&Itemid=73";
                string Trung = "http://www.mailinhexpress.vn/index.php?option=com_content&view=article&id=117&Itemid=72";
                string TayNguyen = "http://www.mailinhexpress.vn/index.php?option=com_content&view=article&id=107&Itemid=74";
                string a = ML(Nam);
                list.Add(a);
                a = ML(Trung);
                list.Add(a);
                a = ML(TayNguyen);
                list.Add(a);
            }
            catch (Exception ex)
            {
                return list;
            }
            return list;
        }
        public string ML(string Url)
        {
            string textLoai="";
            try
            {
                HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(Url);
                // string xpath = "//tbody";
                string xpath = "//div[@id='jv-content']";
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes(xpath))
                {
                    textLoai = link.InnerHtml;
                    break;                    
                }

            }
            catch (Exception ex)
            {
                return textLoai;
            }
            return textLoai;
        }
        [HttpGet]
        public string ThanhBuoi(string ThanhBuoi)
        {
            string a = "";
            try
            {

                string Url = "http://www.thanhbuoi.com.vn/GioiThieu.aspx?id=6";
                HtmlAgilityPack.HtmlWeb web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc = web.Load(Url);
                // string xpath = "//td[@class='News_Default_Right']";
                string xpath = "//div[@class='borderlr pl5 pr5']";

                foreach (HtmlNode link in doc.DocumentNode.SelectNodes(xpath))
                {
                    a = link.InnerHtml;
                    break;
                }

            }
            catch (Exception ex)
            {
                return a;
            }
            return a;
        }
    }
}